package com.intelligentmeterreadingsys.util;

import java.io.File;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.widget.Toast;
import dbhelper.XmlNameHelper;

/**
 * SD卡文件管理帮助类
 * 
 * @author king
 * 
 */
public class FileNameUtil {

    public SharedPreferenceUtil sp;

    private Context context;

    public FileNameUtil(Context context) {
	this.sp = new SharedPreferenceUtil(context);
	this.context = context;
    }

    /**
     * 取得SD卡路径
     * 
     * @return
     */
    public String getSDPath() {
	File sdDir = null;
	boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在

	if (sdCardExist) // 如果SD卡存在，则获取跟目录
	{
	    sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
	} else {
	    Toast.makeText(context, "SD卡不可用", Toast.LENGTH_LONG).show();
	    return null;
	}
	return sdDir.getPath();

    }

    /**
     * 取图片路径
     * 
     * @return
     */
    public String Orderimgpath() {
	String path = "";
	String downfile = sp.getValue(XmlNameHelper.downloadFileName);
	String sd = getImageDatePath();
	if (sd == null) {
	    return null;
	}
	File file = null;
	if (TextUtils.isEmpty(downfile)) {
	    file = new File(sd + "/DownFile/OrderImage");

	    path = sd + "/DownFile/OrderImage";
	} else {
	    file = new File(sd + "/" + downfile + "/OrderImage");
	    path = sd + "/" + downfile + "/OrderImage";
	}
	// 判断文件是否存在
	if (!file.exists()) {
	    file.mkdirs();// 创建文件夹
	}

	return path;

    }

    /**
     * 视频路径
     * 
     * @return
     */
    public String videopath() {
	String path = "";
	String downfile = sp.getValue(XmlNameHelper.downloadFileName);
	String sd = getSDPath();
	if (sd == null) {
	    return null;
	}
	File file = null;
	if (TextUtils.isEmpty(downfile)) {
	    file = new File(sd + "/DownFile/video");

	    path = sd + "/DownFile/video";
	} else {
	    file = new File(sd + "/" + downfile + "/video");
	    path = sd + "/" + downfile + "/video";
	}
	// 判断文件是否存在
	if (!file.exists()) {
	    file.mkdirs();// 创建文件夹
	}

	return path;

    }

    /**
     * 声音路径
     * 
     * @return
     */
    public String soundpath() {
	String path = "";
	String downfile = sp.getValue(XmlNameHelper.downloadFileName);
	String sd = getSDPath();
	if (sd == null) {
	    return null;
	}
	File file = null;
	if (TextUtils.isEmpty(downfile)) {
	    file = new File(sd + "/DownFile/sound");

	    path = sd + "/DownFile/sound";
	} else {
	    file = new File(sd + "/" + downfile + "/sound");
	    path = sd + "/" + downfile + "/sound";
	}
	// 判断文件是否存在
	if (!file.exists()) {
	    file.mkdirs();// 创建文件夹
	}

	return path;

    }

    /**
     * 删除文件
     * 
     * @param name
     */
    public void delectFile(String name) {
	String downfile = sp.getValue(XmlNameHelper.downloadFileName);
	String sd = getSDPath();
	String path = "";
	if (sd == null) {
	    return;
	}

	if (TextUtils.isEmpty(downfile)) {

	    path = sd + "/DownFile" + "/" + name;
	} else {

	    path = sd + "/" + downfile + "/" + name;
	}
	deleteDirectory(path);
    }

    /**
     * 删除文件夹
     * 
     * @param sPath
     * @return
     */
    public boolean deleteDirectory(String sPath) {
	boolean flag = false;
	// 如果sPath不以文件分隔符结尾，自动添加文件分隔符
	// if (!sPath.endsWith(File.separator)) {
	// sPath = sPath + File.separator;
	// }
	File dirFile = new File(sPath);
	// 如果dir对应的文件不存在，或者不是一个目录，则退出
	if (!dirFile.exists() || !dirFile.isDirectory()) {
	    return false;
	}
	flag = true;
	// 删除文件夹下的所有文件(包括子目录)
	File[] files = dirFile.listFiles();
	for (int i = 0; i < files.length; i++) {
	    // 删除子文件
	    if (files[i].isFile()) {
		flag = deleteFile(files[i].getAbsolutePath());
		if (!flag)
		    break;
	    } // 删除子目录
	    else {
		flag = deleteDirectory(files[i].getAbsolutePath());
		if (!flag)
		    break;
	    }
	}
	if (!flag) {
	    return false;
	}
	return true;
	//
	// //删除当前目录
	// if (dirFile.delete()) {
	// return true;
	// } else {
	// return false;
	// }
    }

    /**
     * 删除单个文件
     * 
     * @param sPath
     * @return
     */
    public boolean deleteFile(String sPath) {
	boolean flag = false;
	File file = new File(sPath);
	// 路径为文件且不为空则进行删除
	if (file.isFile() && file.exists()) {
	    file.delete();
	    flag = true;
	}
	return flag;
    }

    /**
     * 抄表图片路径
     * 
     * @return
     */
    public String Waterimgpath() {
	String path = "";
	String downfile = sp.getValue(XmlNameHelper.downloadFileName);
	String sd = getSDPath();
	if (sd == null) {
	    return null;
	}
	File file = null;
	if (TextUtils.isEmpty(downfile)) {
	    file = new File(sd + "/DownFile/WaterImage");

	    path = sd + "/DownFile/WaterImage";
	} else {
	    file = new File(sd + "/" + downfile + "/WaterImage");
	    path = sd + "/" + downfile + "/WaterImage";
	}
	// 判断文件是否存在
	if (!file.exists()) {
	    file.mkdirs();// 创建文件夹
	}

	return path;

    }

    /**
     * 复抄图片路径
     * 
     * @return
     */
    public String Reviewimgpath() {
	String path = "";
	String downfile = sp.getValue(XmlNameHelper.downloadFileName);
	String sd = getSDPath();
	if (sd == null) {
	    return null;
	}
	File file = null;
	if (TextUtils.isEmpty(downfile)) {
	    file = new File(sd + "/DownFile/ReviewImage");

	    path = sd + "/DownFile/ReviewImage";
	} else {
	    file = new File(sd + "/" + downfile + "/ReviewImage");
	    path = sd + "/" + downfile + "/ReviewImage";
	}
	// 判断文件是否存在
	if (!file.exists()) {
	    file.mkdirs();// 创建文件夹
	}

	return path;

    }

    /**
     * 其它设备添加路径
     * 
     * @return
     */
    public String Equipement() {
	String path = "";
	String downfile = sp.getValue(XmlNameHelper.downloadFileName);
	String sd = getSDPath();
	if (sd == null) {
	    return null;
	}
	File file = null;
	if (TextUtils.isEmpty(downfile)) {
	    file = new File(sd + "/DownFile/EquipementImage/add");

	    path = sd + "/DownFile/EquipementImage/add";
	} else {
	    file = new File(sd + "/" + downfile + "/EquipementImage/add");
	    path = sd + "/" + downfile + "/EquipementImage/add";
	}
	// 判断文件是否存在
	if (!file.exists()) {
	    file.mkdirs();// 创建文件夹
	}

	return path;

    }

    /**
     * 其它设备巡检路径
     * 
     * @return
     */
    public String CheckEquipement() {
	String path = "";
	String downfile = sp.getValue(XmlNameHelper.downloadFileName);
	String sd = getSDPath();
	if (sd == null) {
	    return null;
	}
	File file = null;
	if (TextUtils.isEmpty(downfile)) {
	    file = new File(sd + "/DownFile/EquipementImage/check");

	    path = sd + "/DownFile/EquipementImage/check";
	} else {
	    file = new File(sd + "/" + downfile + "/EquipementImage/check");
	    path = sd + "/" + downfile + "/EquipementImage/check";
	}
	// 判断文件是否存在
	if (!file.exists()) {
	    file.mkdirs();// 创建文件夹
	}

	return path;

    }

    /**
     * 其它设备修改图片路径
     * 
     * @return
     */
    public String EquipementModify() {

	String path = "";
	String downfile = sp.getValue(XmlNameHelper.downloadFileName);
	String sd = getSDPath();
	if (sd == null) {
	    return null;
	}
	File file = null;
	if (TextUtils.isEmpty(downfile)) {
	    file = new File(sd + "/DownFile/EquipementImage/modify");

	    path = sd + "/DownFile/EquipementImage/modify";
	} else {
	    file = new File(sd + "/" + downfile + "/EquipementImage/modify");
	    path = sd + "/" + downfile + "/EquipementImage/modify";
	}
	// 判断文件是否存在
	if (!file.exists()) {
	    file.mkdirs();// 创建文件夹
	}

	return path;

    }

    /**
     * 其它设备更换路径
     * 
     * @return
     */
    public String EquipementReplace() {
	String path = "";
	String downfile = sp.getValue(XmlNameHelper.downloadFileName);
	String sd = getSDPath();
	if (sd == null) {
	    return null;
	}
	File file = null;
	if (TextUtils.isEmpty(downfile)) {
	    file = new File(sd + "/DownFile/EquipementImage/replace");

	    path = sd + "/DownFile/EquipementImage/replace";
	} else {
	    file = new File(sd + "/" + downfile + "/EquipementImage/replace");
	    path = sd + "/" + downfile + "/EquipementImage/replace";
	}
	// 判断文件是否存在
	if (!file.exists()) {
	    file.mkdirs();// 创建文件夹
	}

	return path;

    }

    /**
     * 其它设备更换路径
     * 
     * @return
     */
    public String SDCardPath(String name) {
	String path = "";

	String sd = getSDPath();
	if (sd == null) {
	    return null;
	}
	path = sd + "/DownFile" + "/" + name;
	File file = new File(path);
	if (!file.exists()) {
	    file.mkdirs();// 创建文件夹
	}

	return path;

    }

    public String getImageDatePath() {

	File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

	return file.getPath();
    }
}
